package com.dianrui.moonfire.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * Description:修改昵称
 * User: luoziyin
 * Date: 2020-8-17
 */
public class ChangNameActivity extends BaseActivity {
    private String mName;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.edit_activity_change_info)
    EditText editActivityChangeInfo;


    @Override
    public int getContentViewId() {
        return R.layout.activity_change_name;
    }

    @Override
    public void initView() {
        customInit(true);
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle=getIntent().getExtras();
            if (bundle != null) {
                mName = bundle.getString("nickname");
            }
        }
        title.setText(getString(R.string.change_name));
        editActivityChangeInfo.setText(mName);
        topRight.setText(getString(R.string.save));
        topRight.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {
    }

    @OnClick({R.id.back, R.id.top_right, R.id.name_update_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.top_right:
                if (StringUtils.isEmpty(editActivityChangeInfo.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.please_input_names));
                } else {
                    setInfoName(editActivityChangeInfo.getText().toString().trim());
                }
                break;

            case R.id.name_update_img:
                editActivityChangeInfo.setText("");
                break;

            default:
                break;
        }
    }


    private void setInfoName(String value) {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        jsonObject.addProperty("nickname", value);
        XHttpRequest.getInstance().postRequests(Url.SETINFO, jsonObject.toString(),  new XHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject != null &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                                SPUtils.getInstance().put("member_id", jsonObject.optJSONObject("data").optString("member_id"),true);
                                SPUtils.getInstance().put("nickname", jsonObject.optJSONObject("data").optString("nickname"),true);
                                finish();
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }
}
